/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Grafik;

import ModulHåndtering.*;
import java.util.TimerTask;
import java.util.*;

/**
 *
 * @author Charles Thillemann Hemmingsen s122801
 */
public class HovedPanel extends javax.swing.JPanel {

    private MainSkærm ejer;
    private boolean opdaterSensorDataList = false;

    public boolean skalOpdateres() {
        return opdaterSensorDataList;
    }

    /**
     * Creates new form NewJPanel
     */
    public HovedPanel() {
        initComponents();
    }

    //public void timerOpgaver(boolean )
    public void opdaterDataListe() {
        String modulNavn = (String) jList1.getSelectedValue();
        String formattedString;

        String[] dataListe;
        for (Modul m : ejer.butler.getModuler()) {
            if (m.toString().equals(modulNavn)) {
                dataListe = new String[m.sensorList.size()];
                for (int i = 0; i < m.sensorList.size(); i++) {
                    formattedString = String.format("%01d     ---     |  %s", i, m.sensorList.get(i).formatData());
                    dataListe[i] = "Sensor: " + formattedString;
                }
                sensorDataPresenteret.setListData(dataListe);
                break;
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        logoutButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        sensorDataPresenteret = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(500, 500));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 32)); // NOI18N
        jLabel1.setText("<html><center>Velkommen til<br>SmartHome </center> </html>");
        add(jLabel1);
        jLabel1.setBounds(130, 20, 240, 110);

        jButton1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jButton1.setText("Oversigt");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(60, 380, 150, 64);

        jButton2.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jButton2.setText("Indstillinger");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2);
        jButton2.setBounds(310, 380, 150, 64);

        logoutButton.setText("Log ud");
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });
        add(logoutButton);
        logoutButton.setBounds(220, 460, 80, 29);

        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        add(jScrollPane1);
        jScrollPane1.setBounds(60, 140, 200, 200);

        jButton3.setText("Opdater Moduler");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        add(jButton3);
        jButton3.setBounds(190, 350, 140, 29);

        jScrollPane2.setViewportView(sensorDataPresenteret);

        add(jScrollPane2);
        jScrollPane2.setBounds(280, 140, 180, 200);

        jLabel2.setText("Moduler tilknyttet dit hjem ");
        add(jLabel2);
        jLabel2.setBounds(60, 120, 150, 16);

        jLabel3.setText("Seneste måling");
        add(jLabel3);
        jLabel3.setBounds(280, 120, 90, 16);
    }// </editor-fold>//GEN-END:initComponents

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        //Denne her knap tager os til indstillinger og lave listen med ting der kan indstilles
        ejer.rykForest(ejer.indstilinger);

        String modulNavn = (String) jList1.getSelectedValue();

        for (Modul m : ejer.butler.getModuler()) {
            if (m.toString().equals(modulNavn)) {
                ejer.indstilinger.setModul(m);
                break;
            }
        }
        opdaterSensorDataList = false;

        ejer.indstilinger.ugyldigVærdiLabel.setVisible(false);
  }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        // Denne knap fører til oversigt og sætter modulet
        ejer.rykForest(ejer.oversigt);

        String modulNavn = (String) jList1.getSelectedValue();

        for (Modul m : ejer.butler.getModuler()) {
            if (m.toString().equals(modulNavn)) {
                ejer.oversigt.setModul(m);
                break;
            }
        }
        opdaterSensorDataList = false;
        ejer.repaint();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutButtonActionPerformed

        // Denne knap fører tilbage til login skærmen
        ejer.loginPanel.forkertKodeordError.setVisible(false);
        ejer.loginPanel.LoginPasswordField.setText("Password");
        ejer.rykForest(ejer.loginPanel);
        opdaterSensorDataList = false;

    }//GEN-LAST:event_logoutButtonActionPerformed

    /**
     * Opdaterer modulList
     *
     * @param evt
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton3ActionPerformed
    {//GEN-HEADEREND:event_jButton3ActionPerformed

        // Denne knap opdaterer listen over moduler
        ejer.butler.opdaterModuler();
        String[] modulListe = new String[ejer.butler.getModuler().size()];
        for (Modul modul : ejer.butler.getModuler()) {
            modulListe[ejer.butler.getModuler().indexOf(modul)] = modul.toString();
        }
        jList1.setListData(modulListe);
        repaint();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jList1MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jList1MouseClicked
    {//GEN-HEADEREND:event_jList1MouseClicked
        opdaterSensorDataList = true;
        opdaterDataListe();
    }//GEN-LAST:event_jList1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    public javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton logoutButton;
    public javax.swing.JList sensorDataPresenteret;
    // End of variables declaration//GEN-END:variables

    void setEjer(MainSkærm ejer) {
        this.ejer = ejer;
    }
}
